<%--
    Date: 2024-05-22
    教师查看所有的成绩修改申请。审核通过或驳回申请。
    teacherReviewRequests.jsp
    ├── 审核通过 --> teacherHome.jsp
    └── 驳回 --> teacherHome.jsp
--%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/bootstrap.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/teacherReviewRequests.css">
    <script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/jquery-3.7.1.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/updateTime.js"></script>

    <title>教师审核成绩修改申请</title>
    <style>
        body {
            background: url('${pageContext.request.contextPath}/resources/img/bg-HYL.jpg') no-repeat center center fixed;
            background-size: cover;
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            position: relative;
        }
    </style>
</head>
<body>
<div class="overlay"></div>
<div class="top-bar">
    <span id="currentTime"></span>
</div>
<div class="container main-content">
    <h2 class="text-center mt-4 mb-4">审核成绩修改申请</h2>
    <table class="table table-striped table-bordered table-custom">
        <thead>
        <tr>
            <th>学生ID</th>
            <th>科目ID</th>
            <th>申请理由</th>
            <th>请求成绩</th>
            <th>当前状态</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="pendingRequests">
        <c:forEach var="request" items="${pendingRequests}">
            <tr>
                <td>${request.studentId}</td>
                <td>${request.subjectId}</td>
                <td>${request.reason}</td>
                <td>${request.requestedGrade}</td>
                <td>${request.status}</td>
                <td>
                    <button class="btn btn-approve" onclick="reviewRequest(${request.requestId}, 'approved')">批准</button>
                    <button class="btn btn-reject" onclick="reviewRequest(${request.requestId}, 'rejected')">拒绝</button>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
    <div class="text-center mt-3">
        <a href="<c:url value='/teachers/'/>" class="btn btn-secondary">返回教师主页</a>
    </div>
</div>
<script type="text/javascript">
    var teacherId = ${teacherId};
    function reviewRequest(requestId, status) {
        $.ajax({
            url: '<c:url value="/gradeRequest/review"/>',
            type: 'POST',
            data: {
                requestId: requestId,
                status: status
            },
            success: function(response) {
                // 刷新请求列表
                loadPendingRequests();
            }
        });
    }

    function loadPendingRequests() {
        $.ajax({
            url: '<c:url value="/gradeRequest/getPendingRequests/' + teacherId + '"/>',
            type: 'GET',
            success: function(response) {
                // 更新请求列表
                $('#pendingRequests').html('');
                $.each(response, function(index, request) {
                    $('#pendingRequests').append(
                        '<tr>' +
                        '<td>' + request.studentId + '</td>' +
                        '<td>' + request.subjectId + '</td>' +
                        '<td>' + request.reason + '</td>' +
                        '<td>' + request.requestedGrade + '</td>' +
                        '<td>' + request.status + '</td>' +
                        '<td>' +
                        '<button class="btn btn-approve" onclick="reviewRequest(' + request.requestId + ', \'approved\')">批准</button>' +
                        '<button class="btn btn-reject" onclick="reviewRequest(' + request.requestId + ', \'rejected\')">拒绝</button>' +
                        '</td>' +
                        '</tr>'
                    );
                });
            }
        });
    }

    // 页面加载时加载待处理请求
    $(document).ready(function() {
        loadPendingRequests();
    });
</script>

</body>
</html>
